package com.wolf.controller;

import com.wolf.pojo.Wl_commodity;
import com.wolf.pojo.Wl_commodity_inventory;
import com.wolf.service.Wl_commodity_inventoryService;
import com.wolf.util.Math;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Controller
@ComponentScan
public class Wl_commodity_inventoryController {

    JSONObject jsonObject;

    @Autowired
    private Wl_commodity_inventoryService wl_commodity_inventoryService;

    public void setWl_commodity_inventoryService(Wl_commodity_inventoryService wl_commodity_inventoryService) {
        this.wl_commodity_inventoryService = wl_commodity_inventoryService;
    }

    /**
     * 查询所有的库存方法
     * @param modelMap
     * @return
     */
    @RequestMapping("Wl_commodity_inventory_getAll")
    public String getAll(ModelMap modelMap) throws JSONException{

        List<Wl_commodity_inventory> list = wl_commodity_inventoryService.getAll();

        //遍历集合中的商品属性，把json数据进行处理后重新赋值。
        for (Wl_commodity_inventory w:list) {
            JSONObject obj = JSONObject.fromObject(w.getProperty());

            //迭代器
            Iterator it = obj.keys();
            //标识
            int index = 1;
            while (it.hasNext()){
                if(index==1){
                    w.setProperty(null);
                    index=0;
                }
                //获取json中的key
                String key = it.next().toString();
                //获取json中的value
                String value = obj.getString(key);
                //把key和value重新拼接
                if(w.getProperty()==null){
                    w.setProperty(key+":"+value);
                }else{
                    w.setProperty(w.getProperty()+";"+key+":"+value);
                }
            }
        }

        modelMap.addAttribute("commodity_list",list);

        return "/table_wl_commodity_inventory";
    }

    /**
     * 模糊查询
     * @param commodityname
     * @param property
     * @param status
     * @param startprice
     * @param endprice
     * @return
     */
    @RequestMapping("Wl_commodity_inventory_getBean")
    public String getBean(String commodityname,String property,Integer status,Double startprice,Double endprice,ModelMap modelMap){

        if(commodityname=="" || commodityname==null){
            commodityname = null;
        }

        if(property=="" || property==null){
            property = null;
        }

        List<Wl_commodity_inventory> list = wl_commodity_inventoryService.getBean(commodityname,property,status,startprice,endprice);

        //遍历集合中的商品属性，把json数据进行处理后重新赋值。
        for (Wl_commodity_inventory w:list) {

            if(w.getProperty()==null||w.getProperty()==""){
                continue;
            }

            try {
                JSONObject obj = JSONObject.fromObject(w.getProperty());

                //迭代器
                Iterator it = obj.keys();
                //标识
                int index = 1;
                while (it.hasNext()){
                    if(index==1){
                        w.setProperty(null);
                        index=0;
                    }
                    //获取json中的key
                    String key = it.next().toString();
                    //获取json中的value
                    String value = obj.getString(key);
                    //把key和value重新拼接
                    if(w.getProperty()==null){
                        w.setProperty(key+":"+value);
                    }else{
                        w.setProperty(w.getProperty()+";"+key+":"+value);
                    }

                }
            }catch (Exception e){
                continue;
            }

        }

        modelMap.addAttribute("commodity_list",list);

        return "/table_wl_commodity_inventory";

    }

    /**
     * 查询单个对象
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("Wl_commodity_inventory_SelectById")
    public String Wl_commodity_inventory_SelectById(String id,ModelMap modelMap){

        Wl_commodity_inventory wl_commodity = wl_commodity_inventoryService.selectByPrimaryKey(id);

        modelMap.addAttribute("commodity_Bean",wl_commodity);

        return "table_wl_commodity_inventory_update";
    }

    /**
     * 修改
     * @param wl_commodity_inventory
     * @return
     */
    @RequestMapping("Wl_commodity_inventory_update")
    public String update(Wl_commodity_inventory wl_commodity_inventory){

        int i = wl_commodity_inventoryService.updateByPrimaryKeySelective(wl_commodity_inventory);

        return "redirect:Wl_commodity_inventory_getAll";
    }

    /**
     * 修改状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("Wl_commodity_inventory_updateStatus")
    public String updateByStatus(String id,int status){

        if(status==0){
            status = 1;
        }else if(status==1){
            status=0;
        }
        Wl_commodity_inventory wl = wl_commodity_inventoryService.selectByPrimaryKey(id);
        Wl_commodity iddd = wl.getWl_commodity();
        System.out.println(iddd.getId());
        List<Wl_commodity_inventory> list = wl_commodity_inventoryService.getByCommodityId(wl.getWl_commodity().getId(),wl.getProperty());

        for (Wl_commodity_inventory w:list) {

                w.setQuantity(w.getQuantity()+wl.getQuantity());
                w.setUpdatetime(new Date());
                int i = wl_commodity_inventoryService.updateByPrimaryKeySelective(w);
                int is = wl_commodity_inventoryService.deleteByPrimaryKey(id);
                return "redirect:Wl_commodity_inventory_getAll";

        }
        wl.getProperty();

        wl_commodity_inventoryService.updateByStatus(id,status);

        return "redirect:Wl_commodity_inventory_getAll";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("Wl_commodity_inventory_delete")
    public String deleteById(String id){

        wl_commodity_inventoryService.deleteByPrimaryKey(id);

        return "redirect:Wl_commodity_inventory_getAll";
    }

    /**
     * 库存新增
     * @param wl_commodity
     * @param quantity
     * @param property
     * @param price
     * @return
     */
    @RequestMapping("wl_commodity_inventory_insert")
    public String Wl_commodity_inventory_insert(String wl_commodity,Integer[] quantity,String[] property,Integer[] price){
        /**
         * wl_commodity:(Wl_commodity表的主键)
         * quantity:库存数量
         * property:库存规格
         * price:价格
         */
        for (int i =0;i<property.length;i++){
            Wl_commodity_inventory wl_commodity_inventory =new Wl_commodity_inventory();
            Wl_commodity w = new Wl_commodity();
            w.setId(wl_commodity);
            wl_commodity_inventory.setWl_commodity(w);
            if(quantity[i]!=null && quantity[i]!=0){
                wl_commodity_inventory.setQuantity(quantity[i]);
            }
            //把string转换成json数据格式
            String item = "{";
            if(property[i]!="" && property[i]!=null){

                String[] value = null;

                value = property[i].split(";");

                for (String str:value){

                    String[] strs = str.split(":");

                   item = item+"\""+strs[0]+"\":"+"\""+strs[1]+"\",";
                }

                item = item.substring(0,item.lastIndexOf(","))+"}";
                wl_commodity_inventory.setProperty(item);
            }
            if(price[i]!=null ||price[i]!=0){
                wl_commodity_inventory.setPrice(price[i]);
            }
            //随机生成id
            wl_commodity_inventory.setId(Math.random());
            //设置为无效
            wl_commodity_inventory.setStatus(0);
            //设置时间
            wl_commodity_inventory.setCreatetime(new Date());
            //调用新增方法
            int result = wl_commodity_inventoryService.insertSelective(wl_commodity_inventory);

        }
        //跳转到查询所有的controller
        return "redirect:Wl_commodity_inventory_getAll";
    }
}
